<?php
	function urlArgs($str)
	{
		$val = !empty($_GET[$str]) ? $_GET[$str] : null;
		return $val;
	}
	
	function formArgs($str)
	{
		$val = !empty($_POST[$str]) ? $_POST[$str] : null;
		return $val;
	}

	function printError($title,$err)
	{
		exit("<span style='font-size:20px;color:red;'>".$title."</span><hr/><span>".$err."</span>");
	}

	function printWarming($title,$err)
	{
		exit("<span style='font-size:20px;color:#ff9900;'>".$title."</span><hr/><span>".$err."</span>");
	}

	function println($str)
	{
		echo $str.'<br/>';
	}
	
	function zend_ExtractBody($response_str)
	{
		$parts = preg_split('|(?:\r?\n){2}|m', $response_str, 2);
		if (isset($parts[1])) {
			return $parts[1];
		}
		return '';
	}
		
	function phpHttpRequest($url,$parameters,$method)
	{
		$resultObject=Array('IsFsockopen'=>FALSE,'HttpStatus'=>0,'Result'=>'');
		
		$resp_str=NULL;
		$currentURL="http://".$_SERVER ['HTTP_HOST'].$_SERVER['PHP_SELF'];
		$url_=$url;
		$url = parse_url($url);
		
		$fp = @fsockopen($url['host'], $url['port'] ? $url['port'] : 80, $errno, $errstr, 10);
		
		if($fp)
		{
				fputs($fp,sprintf($method." %s%s%s HTTP/1.1\n", $url['path'], $url['query'] ? "?" : "", $url['query']));
				fputs($fp,"Host: $url[host]\n");
				fputs($fp,"Content-type: application/x-www-form-urlencoded\n");
				fputs($fp,"Content-length: " . strlen($parameters) . "\n");
				fputs($fp,"Referer:".$currentURL."\r\n");//伪造部分
				fputs($fp,"Connection: close\n\n");
				fputs($fp,"$parameters \n");
		
				while (!feof($fp))
				{
						$resp_str.=fgets($fp, 128);
				}
				fclose($fp);
		  
				$httpStatus=substr($resp_str,9,3);
				
				$resultObject['IsFsockopen']=TRUE;                        
				$resultObject['HttpStatus']=$httpStatus;                        						
				if($resultObject['HttpStatus']==403)
				{
					$temp=explode("\n",$resp_str);
					$resultObject['Result']=$temp[0];
				}			
				else
				{
					$resultObject['Result']=zend_ExtractBody($resp_str);							
				}		
		}
		return $resultObject;
	}		
		
	function queryWebservice($url,$parameters,$method,$isHaveReturnValue=TRUE)
	{
		if($url==NULL) printError($_SERVER['SCRIPT_NAME'].' WebService调用失败。','错误信息：调用地址为NULL。');
		
		$response=phpHttpRequest($url,$parameters,$method);
		
		if($response['IsFsockopen']==TRUE)
		{
				switch($response['HttpStatus'])
				{
					case 200:
						 if($isHaveReturnValue==TRUE)
						 {
							 if($response['Result']==NULL)
								 printError($_SERVER['SCRIPT_NAME'].' WebService调用失败，返回空值。','WebService地址=>'.$url.'<br/>错误信息，发起WebService请求的程式需要WebService返回值，但该调用并没回像我们期待的那样。<br/>情况1：WebService调用整常，但程序没有返回一个值。<br/>情况2：WebService程序在服务器执行过程中出现错误，出现这种情况就需要调式WebService程式了。');
						 }
						 return $response['Result'];
					break;
		
					case 403:
						printError($_SERVER['SCRIPT_NAME'].' WebService调用失败，服务器拒绝服务。','WebService地址=>'.$url.'<br/>错误信息：'.$response['Result']);
					break;
		
					case 404:
						printError($_SERVER['SCRIPT_NAME'].' WebService调用失败，找不到该服务地址。','WebService地址=>'.$url);
					break;
		
					case 500:
						printError($_SERVER['SCRIPT_NAME'].' WebService调用失败，服务器内部错误。','WebService地址=>'.$url.'<br/>错误信息：'.$response['Result']);
					break;
		
					default:
						printError($_SERVER['SCRIPT_NAME'].' WebService调用失败，这个错误是意料之外的。','WebService地址=>'.$url);
				}
		}
		else
		{
				printError($_SERVER['SCRIPT_NAME'].' WebService调用失败，服务器无效。','WebService地址=>'.$url);
		}			
	}
?>